package com.gitee.secretopen.sso.service.impl;

import cn.chenc.framework.core.model.request.RequestHolder;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.core.model.response.ResultUtil;
import cn.chenc.framework.core.util.JsonUtils;
import cn.chenc.framework.core.util.Md5Util;
import cn.chenc.framework.excel.util.EasyExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.secretopen.sso.common.util.Query;
import com.gitee.secretopen.sso.component.TokenService;
import com.gitee.secretopen.sso.controller.form.PageInfo;
import com.gitee.secretopen.sso.excelmodel.UserUpload;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.secretopen.sso.db.pojo.User;
import com.gitee.secretopen.sso.db.dao.UserMapper;
import com.gitee.secretopen.sso.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author Xjt
 * @Date 2021/2/26 18:53
 * @Version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    TokenService tokenService;

    @Override
    public PageInfo<User> page(User params) {
        IPage<User> page = this.page(
                new Query<User>().getPage(params),
                new QueryWrapper<User>()
                        .select("id","name","create_time","create_by","update_time","update_by","is_login_sso_admin","is_delete")
                        .eq(!StringUtils.isEmpty(params.getName()),
                                "name",params.getName())
                        .between(!ObjectUtils.isEmpty(params.getStartDate()),
                                "create_time",params.getStartDate(),params.getEndDate())
                        .eq("is_delete",0));
        return PageInfo.getInstance(page);
    }

    @Override
    public PageInfo<User> getByName(User params) {
        IPage<User> page = this.page(
                new Query<User>().getPage(params),
                new QueryWrapper<User>()
                        .select("id","name","create_time","create_by","update_time","update_by","is_login_sso_admin","is_delete")
                        .like("name",params.getName())
        );
        return PageInfo.getInstance(page);
    }

    @Override
    public User getLoginUser(){
        HttpServletRequest request= RequestHolder.getRequest();
        User user=tokenService.getAdminLoginUser(request);
        return user;
    }

    @Override
    @Transactional
    public boolean addUser(User user){
        HttpServletRequest request = RequestHolder.getRequest();
        User loginUser=tokenService.getAdminLoginUser(request);
        user.setPassword(Md5Util.MD5("123456",user.getName()));
        user.setCreateTime(LocalDateTime.now());
        user.setCreateBy(loginUser.getName());
        user.setUpdateTime(LocalDateTime.now());
        user.setUpdateBy(loginUser.getName());
        user.setIsLoginSsoAdmin(1);
        user.setIsDelete(0);
        return this.save(user);
    }

    @Override
    public List<User> downloadUsers(){
        List<User> list=this.list();
        return list;
    }

    @Override
    public ResponseVO upload(MultipartFile file) throws Exception{
        HttpServletRequest request = RequestHolder.getRequest();
        User loginUser=tokenService.getAdminLoginUser(request);
        InputStream inputStream = file.getInputStream();
        List<Object> list = EasyExcelUtil.readExcel(inputStream, UserUpload.class);
        String listJson=JsonUtils.objectToJson(list);
        List<User> userList=JsonUtils.jsonToList(listJson,User.class);
        for(User user:userList){
            user.setPassword(Md5Util.MD5("123456",user.getName()));
            user.setCreateTime(LocalDateTime.now());
            user.setCreateBy(loginUser.getName());
            user.setUpdateTime(LocalDateTime.now());
            user.setUpdateBy(loginUser.getName());
            user.setIsLoginSsoAdmin(1);
            user.setIsDelete(0);
        }
        this.saveBatch(userList);
        return ResultUtil.success("导入成功");
    }

}
